package com.wisemoresystem.seal.service;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.SpringTools;

@Service
public class WorkdateService extends BaseService {

	public List<Map<String, Object>> selectmac_wktime(String bank_id) {

		String sql = "select t.*,tb.name,tu.uname from t_mac_wktime_special t "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " left join t_user tu on t.user_id=tu._id "
				+ " where t.status=0 and t.bank_id=? order by t.created asc";

		List<Map<String, Object>> list = jt.queryForList(sql,
				new String[] { bank_id });
		return list;
	}

	/**
	 * 机构是否在工作时间段内
	 */
	public boolean isBankTime(String bank_id) {
		String sql = "select * from t_bank_wktime t where t.status=0 and t.bank_id=? order by t.created desc";
		List<Map<String, Object>> list = jt.queryForList(sql,
				new String[] { bank_id });
		if (ChkTools.isNotNull(list)) {
			Map<String, Object> wk = list.get(0);
			long start = Time.valueOf(wk.get("start_time") + "").getTime();
			long end = Time.valueOf(wk.get("end_time") + "").getTime();
			SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
			long now = Time.valueOf((f.format(new Date()))).getTime();
			if (now >= start && end >= now) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 印控仪是否在工作时间段内
	 */
	public boolean isMacTime(String bank_id) {
		String today = DateTools.getToday();
		// 特殊时
		String sq = "select * from t_mac_wktime t "
				+ "where t.status=0 and t.bank_id=? and t.start_date<=? and t.end_date>=? order by t.created desc ";
		List<Map<String, Object>> list1 = jt.queryForList(sq, new String[] {
				bank_id, today, today });
		if (ChkTools.isNotNull(list1)) {
			Map<String, Object> wk = list1.get(0);
			long start = Time.valueOf(wk.get("start_time") + "").getTime();
			long end = Time.valueOf(wk.get("end_time") + "").getTime();
			SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
			long now = Time.valueOf((f.format(new Date()))).getTime();
			if (now >= start && end >= now) {
				return true;
			}
		}
		// 平常
		String sql = "select * from t_mac_wktime_special t where t.status=0 and t.bank_id=? order by t.created desc";
		List<Map<String, Object>> list = jt.queryForList(sql,
				new String[] { bank_id });
		if (ChkTools.isNotNull(list)) {
			Map<String, Object> wk = list.get(0);

			Calendar cal = Calendar.getInstance();
			int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

			SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
			long now = Time.valueOf((f.format(new Date()))).getTime();
			long start = 0L;
			long end = 0L;
			if (w == 1) {
				start = Time.valueOf(wk.get("wk1") + "").getTime();
				end = Time.valueOf(wk.get("wk1e") + "").getTime();
			} else if (w == 2) {
				start = Time.valueOf(wk.get("wk2") + "").getTime();
				end = Time.valueOf(wk.get("wk2e") + "").getTime();
			} else if (w == 3) {
				start = Time.valueOf(wk.get("wk3") + "").getTime();
				end = Time.valueOf(wk.get("wk3e") + "").getTime();
			} else if (w == 4) {
				start = Time.valueOf(wk.get("wk4") + "").getTime();
				end = Time.valueOf(wk.get("wk4e") + "").getTime();
			} else if (w == 5) {
				start = Time.valueOf(wk.get("wk5") + "").getTime();
				end = Time.valueOf(wk.get("wk5e") + "").getTime();
			} else if (w == 6) {
				start = Time.valueOf(wk.get("wk6") + "").getTime();
				end = Time.valueOf(wk.get("wk6e") + "").getTime();
			} else {
				start = Time.valueOf(wk.get("wk7") + "").getTime();
				end = Time.valueOf(wk.get("wk7e") + "").getTime();
			}
			if (now >= start && end >= now) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		WorkdateService s = SpringTools.getBean(WorkdateService.class);
		System.err.println(s.isBankTime("4028814e4a8f9454014a8f96f18e0004"));
		System.err.println(s.isMacTime("4028814a4a84ad5a014a852806230005"));
	}
}
